package com.ym521.activitystack.core.stack

import android.content.Intent
import androidx.fragment.app.FragmentActivity
import com.ym521.activitystack.annotaion.StackBottom
import java.util.*

/**
 *@author Ym
 * @Email 2435970206@qq.com
 * @QQ 2435970206
 * 管理activity的自定义栈
 */
internal class ActivityStackManagerImpl private constructor() : ActivityStackManager {

    private val activityStack = Stack<FragmentActivity>()

    companion object {
        fun builder(): ActivityStackManager {
            return ActivityStackManagerImpl()
        }
    }

    override fun stackClear(): ActivityStackManagerImpl {
        activityStack.clear()
        return this
    }

    override fun push(fragmentActivity: FragmentActivity) {
        if (isStackBottomActivity(fragmentActivity)) popAll()
        activityStack.push(fragmentActivity)
    }

    /**
     * 插入栈顶
     */
    override fun pushTop(fragmentActivity: FragmentActivity) {
        val topActivity = activityStack.peek()
        if (topActivity != fragmentActivity) {
            if (activityStack.search(topActivity) != -1) {
                activityStack.remove(topActivity)
                activityStack.push(fragmentActivity)
            }
        }
    }

    /**
     * 退栈
     */
    override fun pop(fragmentActivity: FragmentActivity) {
        if (!fragmentActivity.isDestroyed && !fragmentActivity.isFinishing) {
            fragmentActivity.finish()
        }
        if (activityStack.search(fragmentActivity) != -1) {
            activityStack.remove(fragmentActivity)
        }
    }

    override fun <T : FragmentActivity> pop(clazz: Class<T>) {
        activityStack.forEach {
            if (it::class.java.name == clazz.name) {
                return pop(it)
            }
        }
    }

    /**
     * 退栈顶的activity
     */
    override fun pop() {
        val activity = activityStack.pop()
        if (!activity.isDestroyed && !activity.isFinishing) {
            activity.finish()
        }
    }
    /**
     * 打开
     */
    override fun back() {
        val length = activityStack.size
        if (length < 2) return
        val activity = activityStack[length - 2]
        if (!activity.isDestroyed && !activity.isFinishing) {
            val currentActivity = current()
            currentActivity.apply {
                startActivity(Intent(this, activity::class.java))
                finish()
            }
        }
    }

    /**
     * 清空栈
     */
    override fun popAll() {
        while (!activityStack.empty()) {
            val activity = activityStack.pop()
            if (!activity.isDestroyed) {
                activity.finish()
            }
        }
    }

    /**
     * 当前栈顶的activity
     */
    @Synchronized
    override fun current(): FragmentActivity {
        return activityStack.peek()
    }


    private fun isStackBottomActivity(fragmentActivity: FragmentActivity): Boolean {
        val clazz = Class.forName(fragmentActivity.javaClass.name)
        return clazz.isAnnotationPresent(StackBottom::class.java)
    }
//
//    private fun isTransitionActivity(fragmentActivity: FragmentActivity): Boolean {
//        val clazz = Class.forName(fragmentActivity.javaClass.name)
//        return clazz.isAnnotationPresent(Transition::class.java)
//    }

}
